package com.flight.trans.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.NfcBean;
import com.flight.trans.global.Constant;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.JsonUtils;
import com.flight.trans.util.NfcUtils;
import com.flight.trans.util.ToastUtils;
import com.flight.trans.util.voice.SpeakUti;

/**
 * NFC扫描
 */
public class NFCActivity extends BaseActivity {

    private RelativeLayout mRlContainer ;
    private TextView mTvScanTitle;
    private NfcUtils mNfcUtils;

    private String titleName;
    private String Value ;

    @Override
    protected int setContentView() {
        return R.layout.activity_nfc;
    }

    @Override
    protected void initView() {
        mRlContainer = findViewById(R.id.mRlContainer);
        mTvScanTitle = findViewById(R.id.mTvScanTitle);
    }

    @Override
    protected void initData() {
        titleName = mBundle.getString(Constant.Title);
        Value = mBundle.getString(Constant.Value);

        mTvScanTitle.setText(titleName);

        // 初始化NFC功能
        mNfcUtils = new NfcUtils(mActivity);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if(TextUtils.isEmpty(Value)) {
            String fromTag = mNfcUtils.readNFCFromTag(intent);

            if(!TextUtils.isEmpty(fromTag)){
                NfcBean nfcBean = JsonUtils.parseJsonToBean(fromTag, NfcBean.class);

                if(nfcBean != null) {
                    if(mBundle == null)
                        mBundle = new Bundle();
                    mBundle.putSerializable(Constant.INFO, JsonUtils.parseJsonToBean(fromTag , NfcBean.class));
                    onFinish(mBundle , Constant.RESULT_NFC_CODE);
                }else {
                    ToastUtils.showToast(R.string.toast_nfc_card_error);
                }
            }else {
                ToastUtils.showToast(R.string.toast_nfc_card_null);
            }
        }else {
            String msg = mNfcUtils.writeNFCToTag(Value, intent) ? "写入成功" : "写入失败";

            SpeakUti.getInstance().play(msg);
            ToastUtils.showToast(msg);
            onFinish();
        }
    }

    /**
     * 点击事件
     * @param view
     */
    public void onClick(View view){
        if(CommonUtils.isFastDoubleClick())
            return;

        switch (view.getId()){
            case R.id.mBtnCancelScan :
                // 取消扫描
                onFinish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //开启前台调度系统
        if(mNfcUtils.NfcCheck(mActivity)) {
            mNfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);
        }else {
            onFinish();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        //关闭前台调度系统
        if(mNfcUtils.NfcCheck(mActivity)) {
            mNfcUtils.mNfcAdapter.disableForegroundDispatch(this);
        }else {
            onFinish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mNfcUtils = null;
    }
}
